import os
import cv2
import numpy


def get_array(value, offset):
    low = value - offset if value - offset > 0 else 1
    high = value + offset
    return [low, value, high]


def splitPic(image, center, output_path, step_size=60):
    if not os.path.exists(output_path):
        os.mkdir(output_path)
    index = 0
    for offset in numpy.random.randint(4, 16, 5):
        for height in get_array(center[1], offset):
            for width in get_array(center[0], offset):
                cropImg = image[height:height + 2 * step_size,
                          width:width + 2 * step_size]  # height , width
                cropImg = cv2.resize(cropImg, (64, 64), interpolation=cv2.INTER_CUBIC)
                cv2.imwrite(output_path + "\\" + str(index) + ".jpg", cropImg)
                index += 1


# when you have a skin pic whose dimension is 980x500,
# you can use this method,
#
if __name__ == '__main__':
    # give me a dir path where saved a list of skin_pic
    root_path = "D:\\LOL_DATA\\heroSkin3"
    # give me a list of center point where you want to focus
    center_point = [[591, 62], [517, 42], [405, 31], [505, 116], [612, 120],
                    [447, 114], [683, 109], [611, 53], [578, 80], [485, 49],
                    [688, 51], [541, 85], [625, 138],

                    [730, 110], [717, 73], [722, 80], [60, 36], [313, 121],
                    [459, 98], [601, 109], [459, 22], [614, 132], [679, 128],
                    [697, 120], [665, 50], [673, 79], [552, 70], [680, 163],

                    [419, 91], [549, 19], [732, 58], [457, 31], [650, 85],
                    [732, 92], [596, 220], [508, 109], [680, 39], [715, 52],
                    [574, 137], [548, 14], [551, 52], [548, 132], [781, 112],
                    [658, 100], [652, 66],

                    [711, 130], [412, 70], [588, 88], [574, 49], [570, 67],
                    [698, 52], [663, 61], [527, 133], [549, 162], [282, 129],
                    [447, 184], [172, 54], [689, 242], [752, 118], [667, 42],

                    [534, 116], [600, 90], [575, 89], [699, 24], [622, 75],
                    [687, 197], [645, 171], [540, 137], [260, 272], [552, 90],
                    [521, 173], [720, 127], [675, 132], [453, 51], [893, 167],
                    [748, 101], [619, 89], [593, 63], [775, 165], [731, 61],

                    [488, 227], [777, 435], [543, 148], [690, 25], [429, 142],
                    [587, 1], [589, 44], [426, 122], [572, 82], [585, 143],
                    [393, 24], [510, 139], [431, 136], [460, 46], [579, 163],

                    [704, 85], [595, 78], [716, 101], [502, 33], [578, 125],
                    [541, 39], [637, 99], [705, 22], [633, 110], [642, 82],
                    [582, 32], [425, 161], [645, 161], [581, 89], [464, 43],

                    [789, 40], [595, 144], [687, 107], [478, 45], [698, 56],
                    [563, 124], [758, 19], [490, 49], [464, 110], [534, 84],
                    [374, 173], [598, 95], [662, 74], [544, 164], [414, 203],

                    [643, 54], [509, 50], [798, 118], [540, 135], [470, 177],
                    [631, 130], [567, 99], [709, 43], [579, 30], [717, 317],
                    [553, 71], [601, 144], [408, 19], [574, 80], [230, 81],
                    [578, 3], [399, 48], [576, 223], [542, 33], [520, 189],
                    [520, 96], [462, 55], [713, 126]

                    ]
    # default step size = 64
    step_size = [64, 60, 58, 80, 96,
                 64, 80, 96, 50, 64,
                 40, 50, 50,

                 45, 45, 64, 60, 72,
                 108, 72, 64, 55, 50,
                 60, 55, 38, 60, 75,

                 96, 72, 68, 40, 92,
                 72, 110, 50, 64, 45,
                 128, 55, 72, 108, 50,
                 40, 60,

                 40, 84, 45, 52, 50,
                 40, 64, 108, 64, 40,
                 50, 96, 50, 108, 96,

                 160, 45, 45, 45, 40,
                 40, 64, 50, 64, 72,
                 84, 64, 50, 96, 40,
                 45, 60, 110, 140, 38,

                 72, 42, 45, 64, 64,
                 72, 68, 80, 90, 50,
                 64, 88, 88, 108, 84,

                 45, 90, 72, 45, 64,
                 55, 35, 45, 45, 60,
                 50, 64, 88, 55, 50,

                 65, 52, 26, 200, 64,
                 64, 50, 140, 64, 96,
                 84, 55, 72, 64, 72,

                 65, 52, 50, 80, 102,
                 38, 40, 64, 72, 96,
                 72, 55, 40, 50, 40,
                 50, 140, 96, 72, 108,
                 55, 84, 30

                 ]
    # output dir setting

    for skin_pic_path, center, step in zip(os.listdir(root_path), center_point, step_size):
        output_path = root_path + "\\results"
        if not os.path.exists(output_path):
            os.mkdir(output_path)
        original_image = cv2.imread(root_path + "\\" + skin_pic_path)
        splitPic(original_image, center,
                 output_path + "\\" + skin_pic_path.split(".")[0], step)
